package com.rjj.wlqq.wx.controller;

import cn.hutool.http.HttpRequest;
import com.alibaba.fastjson.JSON;
import com.rjj.wlqq.wx.entity.template.Content;
import com.rjj.wlqq.wx.entity.template.Template;
import com.rjj.wlqq.wx.entity.template.WxTemplateResponse;
import com.rjj.wlqq.wx.entity.template.ticket.Ticket;
import com.rjj.wlqq.wx.entity.template.ticket.TicketRequest;
import com.rjj.wlqq.wx.service.WxService;
import com.rjj.wlqq.wx.tool.WxKit;
import org.apache.commons.io.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.InputStream;
import java.net.URLEncoder;

/**
 * @author r
 * @date 2020/12/26 22:10
 */
@RestController
public class WxController
{
    @Autowired
    private WxKit wxKit;


    @Autowired
    private WxService wxService;

    /**
     * 获取token
     */
    @RequestMapping("getToken")
    public String getToken()
    {
        return wxKit.getAccessToken();
    }

    /**
     * 发送模版消息
     */
    @RequestMapping("sendMsg")
    public String sendTemplateMsg(String message1, String message2)
    {
        //final WxTemplateResponse wxTemplateResponse = wxService.sendTemplateMessage("oGrEq548WlQdgaOK5NRamXQKNkmk", "先生", "吃饭了", "http://www.baidu.com");
        final WxTemplateResponse wxTemplateResponse = wxService.sendTemplateMessage("oGrEq548WlQdgaOK5NRamXQKNkmk", message1, message2, "http://www.baidu.com");
        return JSON.toJSONString(wxTemplateResponse);
    }

    /**
     * 发送模版消息
     */
    @RequestMapping("sendMessage")
    public String sendTemplateMessage(String message1, String message2)
    {
        final Template template = Template.Builder()
                .setTemplate_id("iDXAYYD3sTDpf2zBMw57VqvtK_wrE_ODLz0x2jYU4lY")
                .setTouser("oGrEq548WlQdgaOK5NRamXQKNkmk")
                .setUrl("http://www.baidu.com")
                .setData("first", Content.Builder().setValue(message1).setColor("#173177").build())
                .setData("keywork", Content.Builder().setValue(message2).setColor("#173177").build());

        final WxTemplateResponse wxTemplateResponse = wxService.sendTemplateMessage(template);
        return JSON.toJSONString(wxTemplateResponse);
    }

    /**
     * 获取二维码
     */
    @RequestMapping("getTicket")
    public String getTicket(HttpServletResponse response) throws Exception
    {
        final Ticket ticketResponse = wxService.getTicketResponse(TicketRequest.getSceneStr(604800L, "sfsfsdf"));

        downloadPic(response, ticketResponse);
        return JSON.toJSONString(ticketResponse);
    }

    /**
     * 下载图片
     */
    public void downloadPic(HttpServletResponse response, Ticket ticketResponse) throws Exception
    {
        final String url = "https://mp.weixin.qq.com/cgi-bin/showqrcode?ticket=" + URLEncoder.encode(ticketResponse.getTicket(), "UTF-8");
        final InputStream inputStream = HttpRequest.get(url)
                .execute().bodyStream();

        //下载到指定位置
        // DownloadKit.downloadFileFromURL(url, new File("/Users/renjianjun/study/ideaWorkSpace/wlqq/download/errorExcel/a.png"));

        //直接显示网页上
        IOUtils.copy(inputStream, response.getOutputStream());

        //网页响应下载图片
//        response.setContentType("application/force-download;charset=UTF-8");
//        response.addHeader("Content-Disposition", "attachment;filename=aa.png");
//        final ServletOutputStream outputStream = response.getOutputStream();
//        IOUtils.copy(inputStream, outputStream);
    }
}
